import filterData from '@/common/filterData';
import formatter from '@/common/formatter';
import mergeData from '@/common/mergeData';
import department from '@/common/getDepartment';
const { department1, department2, departOrder } = department;

const r_g_Obj = department1.reduce((p, c, i) => {
	p[c] = department2[i].join(',');
	return p;
}, {})
let grids = department2.flat();
import Decimal from "decimal.js";
const base = { title: '返乡人员数' }
const dataset = {
	source: [
		['日期', '2023年返乡人数', '2024年返乡人数', '2024年返乡人数占比'],
		['茂南', 186, 100, 0.32]
	]
};


const seriesTemplate = [['bar', 'bar'], ['line', 'line']];

function getChartData([dataPool1, dataPool2], condition, mergeDepartment = ['全市'], date) {

	let isAllGrid = (condition && condition.length) ? (condition[1].length === 0 || condition[1].length === 31) : true;
	// let series = isAllGrid ? ['bar', 'bar'] : ['bar', 'bar', 'line', 'line'];
	// this.series = series.map((type, index) => ({ type, yAxisIndex: index }));
	let orderData = [
		`nlPrevFXRS@${NLObj.prevYear}返乡人数`,
		`nlThisFXRS@${NLObj.thisYear}返乡人数`,
		`nlPrevZhanBi@${NLObj.prevYear}返乡人数占比`,
		`nlThisZhanBi@${NLObj.thisYear}返乡人数占比`
	].map((collection) => {
		const [coll, title] = collection.split('@');
		return [['日期', title]].concat(dataPool2[coll].map(i => {
			let date = formatter.solarLunarDate(i['日期']);
			if (isAllGrid) {
				let totalZhanbi = grids.reduce((p, c) => {
					return p.plus(new Decimal(i[c] || 0));
				}, new Decimal(0)).toNumber();
				return [date, title.endsWith('占比') > 0 ? i['开发区'] !== undefined ? totalZhanbi : 0 : i["全市"]];
			}
			let value = condition[1].reduce((p, c) => {
				return p.plus(new Decimal(i[c] || 0));
			}, new Decimal(0)).toNumber();


			return [date, value || '-'];
		}))
	})

	// 补充 因为闰年 导致的【冬月三十】和【腊月三十】缺失
	orderData = orderData.map((item) => {
		// 如果大于9 小于 40 则说明过了冬月，未到腊月三十
		if (item.length > 9 && item.length < 40) {
			let dongYue30Index = item.findIndex((i) => i[0] === '冬月三十');
			if (dongYue30Index < 0) {
				let dony29Index = item.findIndex((i) => i[0] === '冬月廿九');
				dony29Index >= 0 && item.splice(dony29Index + 1, 0, ['冬月三十', '']);
			}
		} else if (item.length >= 40) {
			// 如果大于等于于 40 则说明过了冬月，到腊月三十
			let dongYue30Index = item.findIndex((i) => i[0] === '冬月三十');
			if (dongYue30Index < 0) {
				let dony29Index = item.findIndex((i) => i[0] === '冬月廿九');
				dony29Index >= 0 && item.splice(dony29Index + 1, 0, ['冬月三十', '']);
			}

			let laYue30Index = item.findIndex((i) => i[0] === '腊月三十');
			if (laYue30Index < 0) {
				let lay29Index = item.findIndex((i) => i[0] === '腊月廿九');
				lay29Index >= 0 && item.splice(lay29Index + 1, 0, ['腊月三十', '']);
			}
		}
		let newItem = item.map((i) => [i[0], i[1] || '']);
		return newItem;
	});
	const data = mergeData.mergeSheets(orderData);

	if (data.length > 7) {
		this.dataZoom[0].show = true;
	}
	this.dataset.source = data;
	return data;
}

// ======更新内容↑========

const series = seriesTemplate.flatMap((arr, index) =>
	arr.map(type => ({ type, yAxisIndex: index }))
);

const source = dataset.source;

export default {
	title: {
		text: base.title,
		left: 'center'
	},
	legend: {
		type: 'scroll',
		top: 30
	},
	tooltip: {
		trigger: window.isMobile ? 'item' : 'axis',
		axisPointer: {
			type: 'cross',
			crossStyle: {
				color: '#999'
			}
		},
		formatter: (params) => {
			if (window.isMobile) {
				const value = params.value[params.dimensionNames.indexOf(params.seriesName)];
				let tip = `<div class="space-y-1 px-2"><div class='font-bold'>${params.name}</div>`;
				tip += `<div class='flex justify-between space-x-6 p-0'>
        <span>${params.marker + params.seriesName}</span>
        <span class="font-bold">${value ? formatter.valueToText(value, params.seriesName.includes('占比')) : '-'}</span>
        </div>`
				tip += '</div>'
				return tip
			} else {
				let tip = `<div class="space-y-1 px-2"><div class='font-bold'>${params[0].name}</div>`;
				params.forEach((item, index) => {
					const value = item.value[item.dimensionNames.indexOf(item.seriesName)];
					const r = item.seriesName.endsWith('占比');
					tip += `<div class='flex justify-between space-x-6 p-0'>
        <span>${item.marker + item.seriesName}</span>
        <span class="font-bold">${value && value !== '-' ? formatter.valueToText(value, r, r ? 2 : void 0) : '-'}</span >
        </div > `
				});
				//  <span class="font-bold">${value && value !== '-' ? formatter.valueToText(value, r, r ? 4 : void 0) : '-'}</span >
				tip += '</div>'
				return tip
			}
		}
	},
	grid: {
		width: '96%',
		height: '80%',
		left: '2%',
		right: '2%',
		top: '16%',
		bottom: '2%',
		containLabel: true
	},
	dataset,
	dataZoom: [{ show: false }],
	xAxis: {
		type: 'category',
		axisPointer: {
			type: 'shadow'
		}
	},
	yAxis: [
		{
			type: 'value',
			name: ''
		}, {
			type: 'value',
			name: '',
			axisLabel: {
				formatter: (value) => formatter.valueToText(value, true)
			},
			axisPointer: {
				label: {
					formatter: (params) => formatter.valueToText(params.value, true)
				}
			}
		}
	],
	series,
	getChartData,
}